package eval.host.compare;


import java.util.Collections;
import java.util.List;
import com.google.inject.Singleton;
import eval.host.Host;
import eval.utilities.configuration.ExperimentConfiguration;

/**
 * This class is used to sort the hosts in the cloud,
 * according to the delta between the current CPU load and the minimum line
 *  in each host.
 * 
 * @author Amit Peled   <amit.kapina@gmail.com>
 * @author Igal Kaufman <igal.kaufman@gmail.com> 
 */

@Singleton
public class MinLineHostSortList implements ISortHostList{
	protected ExperimentConfiguration conf = ExperimentConfiguration.getSingleton();

	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	@Override
	public int compare(Host host1, Host host2) {
		double hostLoad = host1.getCurrentCpuLoad();
		double hostCapcity = host1.getHostCapacity().getCpuCapacity();
		double minLine = (host1.getHostCapacity().getMinLine()/100.0)*hostCapcity;
		double deltaHost1 = minLine - hostLoad;

		hostLoad = host2.getCurrentCpuLoad();
		hostCapcity = host2.getHostCapacity().getCpuCapacity();
		minLine = (host2.getHostCapacity().getMinLine()/100.0)*hostCapcity;
		double deltaHost2 = minLine - hostLoad;
		return (int)(deltaHost1-deltaHost2);

	}

	/* (non-Javadoc)
	 * @see eval.host.compare.ISortHostList#sortHostList(java.util.List)
	 */
	@Override
	public List<Host> sortHostList(List<Host> hostToSort) {
		Collections.sort(hostToSort,this);
		return hostToSort;
	}

}
